package database;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Vector;

import object.Specialized;
import utility.StringUtil;
import clientMain.Login;
import clientMain.Main;

public class SpecializedDelegate {
	
	public Vector<Specialized> getListSpec(int page, String sort, boolean flag) {
		Vector<Specialized> list = new Vector<Specialized>();
		try {
			Login.conn.setAutoCommit(false);
			CallableStatement cst = Login.conn.prepareCall("{ call sp_ListSpec(?,?,?,?) }");
			cst.setInt(1, Main.record);
			cst.setInt(2, page);
			cst.setString(3, sort);
			cst.setBoolean(4, flag);
			ResultSet rs = cst.executeQuery();
			while(rs.next()){
				Specialized specialized = new Specialized();
				specialized.setSpecId(rs.getInt("Specialized_Id"));
				specialized.setSpecName(StringUtil.getFilterIllegal(rs.getString("Specialized_Name")));
				if(page != 0){
					specialized.setSpecDes(StringUtil.getFilterIllegal(rs.getString("Specialized_Des")));
				}
				list.add(specialized);
			}
			Login.conn.commit();
		} catch (Exception e) {
			e.printStackTrace();
			list = null;
		}
		return list;
	}
	public boolean insertSpe(Specialized spe, int userInsert){
		boolean flag = false;
		try {
			Login.conn.setAutoCommit(false);
			CallableStatement cst = Login.conn.prepareCall("{call sp_InsertSpec(?,?,?)}");
			cst.setString(1, StringUtil.setFilterIllegal(spe.getSpecName()));
			cst.setString(2, StringUtil.setFilterIllegal(spe.getSpecDes()));
			cst.setInt(3, userInsert);
			cst.executeUpdate();
			Login.conn.commit();
			flag = true;
		} catch (Exception e) {
			e.printStackTrace();
			flag = false;
		}
		return flag;
	}
	public boolean updateSpe(Specialized Spe,int userUpdate){
		boolean flag = false;
		try {
			Login.conn.setAutoCommit(false);
			CallableStatement cst = Login.conn.prepareCall("{call sp_UpdateSpec(?,?,?,?)}");
			cst.setInt(1, Spe.getSpecId());
			cst.setString(2, StringUtil.setFilterIllegal(Spe.getSpecName()));
			cst.setString(3, StringUtil.setFilterIllegal(Spe.getSpecDes()));
			cst.setInt(4, userUpdate);
			cst.executeUpdate();
			Login.conn.commit();
			flag = true;
		} catch (Exception e) {
			e.printStackTrace();
			flag = false;
		}
		return flag;
	}
	public boolean CheckInsertSpeName(String name){
		boolean rs = false;
		try {	
			  Statement st = Login.conn.createStatement();
			  ResultSet  traVe = st.executeQuery("select  * from Specialized where Specialized_Name  =N'"+name+"'");
			  while (traVe.next()) { 
				  rs = true;
			  }
			  return rs;
			  
		} catch (Exception e) {
			
		}
		return rs;
	}
}
